package com.dataseek.iot.mysql.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dataseek.iot.core.util.DateUtil;
import com.dataseek.iot.mysql.entity.Device;
import com.dataseek.iot.mysql.entity.DeviceRecord;
import com.dataseek.iot.mysql.entity.Dict;
import com.dataseek.iot.mysql.entity.ProductParams;
import com.dataseek.iot.mysql.mapper.DeviceRecordMapper;
import com.dataseek.iot.mysql.service.IDeviceParamsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cc
 * @since 2021-09-17
 */
@Service
public class DeviceRecordServiceImpl extends ServiceImpl<DeviceRecordMapper, DeviceRecord> implements IDeviceParamsService {

    @Autowired
    DeviceServiceImpl deviceService;

    @Autowired
    DictServiceImpl dictService;

    @Autowired
    ProductParamsServiceImpl productParamsService;

    /**
     * 获取过去几天的数据
     *
     * @param code
     * @param num
     * @return
     */
    public List<DeviceRecord> getRecordsInPast(String code, Integer num) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("device_code", code);
        wrapper.orderByDesc("create_time");
        wrapper.gt("create_time", DateUtil.getPastDate(num));
        return this.list(wrapper);
    }

    public List<DeviceRecord> getRecordsInDay(String code, Integer pastDay) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("device_code", code);
        wrapper.orderByDesc("create_time");
        wrapper.gt("create_time", DateUtil.getPastDate(pastDay));
        StringBuffer sb = new StringBuffer();
        Device d = deviceService.getDeviceByCode(code);
        List<String> names = new ArrayList<>();
        if (Objects.isNull(d)) {
            log.error("未查询到数据");
            return null;
        }

        List<ProductParams> params = productParamsService.getAllByProductId(d.getProductId());
        for (ProductParams param : params) {
            Dict dict = dictService.getById(param.getDictId());
            names.add(dict.getParamCode());
        }

        for (int i = 0; i < names.size(); i++) {
            sb.append(names.get(i)).append(", ");
        }
        sb.append(" timestamp, create_time");
        wrapper.select(sb.toString());
        return this.list(wrapper);
    }

    public List<DeviceRecord> getRecordsInHour(String code, Integer hour) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("device_code", code);
        wrapper.orderByDesc("create_time");
        wrapper.gt("create_time", DateUtil.getPastTime(hour));
        StringBuffer sb = new StringBuffer();
        Device d = deviceService.getDeviceByCode(code);
        List<String> names = new ArrayList<>();
        if (Objects.isNull(d)) {
            log.error("未查询到数据");
            return null;
        }

        List<ProductParams> params = productParamsService.getAllByProductId(d.getProductId());
        for (ProductParams param : params) {
            Dict dict = dictService.getById(param.getDictId());
            names.add(dict.getParamCode());
        }

        for (int i = 0; i < names.size(); i++) {
            if(names.get(i).equals("In")){
                sb.append("Inn").append(", ");
            }else {
                sb.append(names.get(i)).append(", ");
            }
        }
        sb.append(" timestamp, create_time");
        wrapper.select(sb.toString());
        List list = this.list(wrapper);
        if(list.size()>0){
            return list;
        }else {
            wrapper.gt("create_time", DateUtil.getPastTime(hour));
            return this.list(wrapper);
        }
    }

    public List<DeviceRecord> getPropertyHistory(String code, String property, Integer pastDay) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("create_time");
        wrapper.eq("device_code", code);
        wrapper.gt("create_time", DateUtil.getPastDate(pastDay));
        wrapper.select(property, "create_time");
        return this.list(wrapper);
    }

    public List<DeviceRecord> getPropertyHistoryByHour(String code, String property, Integer hour) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("create_time");
        wrapper.eq("device_code", code);
        wrapper.gt("create_time", DateUtil.getPastTime(hour));
        wrapper.select(property, "create_time");
        return this.list(wrapper);
    }

}

